using System;
using CStock;
using System.Diagnostics;
using CUtil;

using System.ComponentModel;
using System.Collections.Generic;

namespace CStock
{
	/// <summary>
	/// Summary description for TradeSystemBase - serializable class
	/// </summary>
	/// 
	//[System.Xml.Serialization.XmlRootAttribute("TradeSystem", Namespace="", IsNullable=false)]
	[Serializable]
    public class TradeSystemBase : Profile, ITradeSystemBase
	{
        
     

		#region private Data

        protected double bidMoreOrLessInPercent;

		private System.Collections.ArrayList buyConditionsCollection; // array of TradeConditions
		private System.Collections.ArrayList sellConditionsCollection;// array of TradeConditions

        protected IList<IOptimizationResult> optimizationResults;
   

		#endregion	

		

		#region constructor, dispose
		
		public TradeSystemBase()
		{
            ResetPathFromConfiguration();

            this.iD = CUtil.TradeSystemFiles.Trade.ToString();
			this.fileName = CUtil.TradeSystemFiles.Trade.ToString() + ".xml";
			this.buyConditionsCollection = new System.Collections.ArrayList();
			this.sellConditionsCollection = new System.Collections.ArrayList();


           
			
		}
        public override bool Serialize()
        {
            ResetPathFromConfiguration();
            return base.Serialize();
        }
        public override bool ResetPathFromConfiguration()
        {

            ProgramContext pc = ProgramContext.Instance;
            Debug.Assert(pc != null);
            if (pc != null)
                this.path = pc.Configuration.Directories.TradeSystemsTemplatesPath;
            return true;

        }
		#endregion

		#region public properties


        public double BidMoreOrLessInPercent
        {
            get
            {

                return this.bidMoreOrLessInPercent;
            }
            set
            {
                this.bidMoreOrLessInPercent = value;

            }
        }
		//[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Serialization.XmlForm.Unqualified, IsNullable=false)]
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public ITradeCondition[] BuyConditions
		{
			get
			{
				return (TradeCondition[])buyConditionsCollection.ToArray(typeof(TradeCondition));
			
			}
			set
			{
				buyConditionsCollection.Clear();
				for(int i = 0; i < value.Length; i++)
				{
					buyConditionsCollection.Add(value[i]);
				}
			}
		}
	
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public ITradeCondition[] SellConditions
		{
			get
			{
				return (TradeCondition[])sellConditionsCollection.ToArray(typeof(TradeCondition));
			}
			set
			{
				sellConditionsCollection.Clear();
				for(int i = 0; i < value.Length; i++)
				{
					sellConditionsCollection.Add(value[i]);
				}
			}
		}
		//[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Serialization.XmlForm.Unqualified, IsNullable=false)]
		//[System.Xml.Serialization.XmlIgnoreAttribute()]
        public TradeCondition[] TradeConditions
		{
			get
			{
				System.Collections.ArrayList tradeCond = new System.Collections.ArrayList();
				foreach(TradeCondition tc in buyConditionsCollection)
				{
					tradeCond.Add(tc);
				}
				foreach(TradeCondition tc in sellConditionsCollection)
				{
					tradeCond.Add(tc);
				}
			
			
				return (TradeCondition[])tradeCond.ToArray(typeof(TradeCondition));
			}
			set // provide empty setter - otherwise this won't be XML serialized
			{
                TradeCondition[] tradCond = value as TradeCondition[];
                buyConditionsCollection = new System.Collections.ArrayList();
                sellConditionsCollection = new System.Collections.ArrayList();

                for (int i = 0; i < tradCond.GetLength(0); i++)
                {
                    TradeCondition tc = tradCond[i];
                    if (tc.BuyOrSell == "Buy")
                    {
                        buyConditionsCollection.Add(tc);
                    }
                    else
                    {
                        sellConditionsCollection.Add(tc);
                    }

                }


			}
	
		}
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public IList<IOptimizationResult> OptimizationResults
		{
			get
			{
				return this.optimizationResults;
			}
			set
			{
                this.optimizationResults = value;
			}
		}
		public void AddBuyCondition(ITradeCondition my)
		{
			buyConditionsCollection.Add(my);
            
		}
		public void AddSellCondition(ITradeCondition my)
		{
			sellConditionsCollection.Add(my);
		}


	
		#endregion

		#region nonserialzable
		//I only need them for the copy constructor
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public System.Collections.ArrayList BuyConditionsCollection
		{
			get
			{
				return buyConditionsCollection;
			}
			set
			{
				buyConditionsCollection = value;
			}
	
		}
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public System.Collections.ArrayList SellConditionsCollection
		{
			get
			{
				return sellConditionsCollection;
			}
			set
			{
				sellConditionsCollection = value;
			}
	
		}
		#endregion

		#region CopyConstructor - only for serializing !!


		#endregion

    
      

	}
}
